/*
 * @Author: szx
 * @Date: 2021-11-09 12:53:09
 * @LastEditTime: 2021-11-09 13:11:38
 * @Description:
 * @FilePath: \leetcode\1-99\98\98.cpp
 */
#include <iostream>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right)
        : val(x), left(left), right(right) {}
};
class Solution {
   public:
    bool helper(TreeNode *root, long long lower, long long upper) {
        if (root == nullptr) {
            return true;
        }
        if (root->val <= lower || root->val >= upper) {
            return false;
        }
        return helper(root->left, lower, root->val) &&
               helper(root->right, root->val, upper);
    }
    bool isValidBST(TreeNode *root) { return helper(root, LONG_MIN, LONG_MAX); }
};

class Solution {
   public:
    long long pre = LLONG_MIN;

    bool isValidBST(TreeNode *root) {
        if (!root) return true;

        if (!isValidBST(root->left)) return false;

        if (root->val <= pre) return false;
        pre = root->val;

        if (!isValidBST(root->right)) return false;

        return true;
    }
};